package com.liy.cloud.base.rest;

import com.liy.cloud.base.entity.BaseEquParamEntity;
import com.liy.cloud.base.entity.BaseEquTypeParamEntity;
import com.liy.cloud.base.entity.BaseTypeParamEntity;
import com.liy.cloud.base.service.BaseEquParamService;
import com.liy.cloud.base.service.BaseEquTypeParamService;
import com.liy.cloud.base.service.BaseTypeParamService;
import com.das.framework.common.annotation.ActionPermission;
import com.das.framework.common.annotation.MethodPermission;
import com.das.framework.common.extvo.ResultVO;
import com.das.framework.common.utils.Tools;
import com.das.framework.common.web.controller.BaseCrudController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;


/**
 * @Author: zhangw
 * @Date: 2018/6/22  9:08
 * @Description: 参数信息管理
 **/
@RestController
@RequestMapping("/typeparam")
@ActionPermission(code = "BASE.TYPEPARAM", description = "参数信息管理")
public class BaseTypeParamRest extends BaseCrudController<BaseTypeParamService, BaseTypeParamEntity>{

    protected static Logger log = LoggerFactory.getLogger(BaseTypeParamRest.class);

    @Value("${server.port}")
    private String port;
    @Autowired
    private BaseTypeParamService baseTypeParamService;
    @Autowired
    private BaseEquTypeParamService baseEquTypeParamService;
    @Autowired
    private BaseEquParamService baseEquParamService;



    @RequestMapping(value = {"/addBaseTypeParam"}, method = {RequestMethod.POST})
    @MethodPermission(codes = {"INSERT"}, description = "新增")

    public ResultVO addBaseTypeParam(@RequestBody @Valid BaseTypeParamEntity baseTypeParamEntity) {

        try {

            //新增前先校验编号是否重复
            BaseTypeParamEntity typeParam = this.baseService.findById(baseTypeParamEntity.getId());
            if (typeParam != null) {
                return new ResultVO(false, "参数编号存在重复，请重新输入", "500");
            }

            baseTypeParamService.addBaseEquInfo(baseTypeParamEntity);
            return new ResultVO(true, "新增成功", "200", baseTypeParamEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new ResultVO(false, "新增时发生错误:" + e.getMessage(), "500");
        }
    }

    /**
     * @Description: 批量删除
     **/
    @PostMapping("/delete/batch")

    public ResultVO deleteBatch(@RequestBody() List<String> ids) {
        try {
            for(String paramId : ids) {
                //删除前先判断是否有关联的种类
                BaseEquParamEntity baseEquParam = new BaseEquParamEntity();
                baseEquParam.setId(paramId);
                List<BaseEquParamEntity> equParamList = baseEquParamService.findList(baseEquParam);
                if (equParamList.size() > 0) {
                    return new ResultVO(false, "该参数编号:"+paramId+",存在和种类关联，请查看【设备种类参数管理】!", "500");
                }
                //删除前先判断是否有关联的设备
                BaseEquTypeParamEntity baseEquTypeParam = new BaseEquTypeParamEntity();
                baseEquTypeParam.setId(paramId);
                List<BaseEquTypeParamEntity> typeParamList = baseEquTypeParamService.findList(baseEquTypeParam);
                if (typeParamList.size() > 0) {
                    return new ResultVO(false, "该参数编号:"+paramId+",存在和设备关联，请查看【设备参数管理】!", "500");
                }
            }
            baseTypeParamService.deleteBatch(ids);

            return new ResultVO(true, "删除成功:", "200");
        } catch (Exception var4) {
            log.error(var4.getMessage(), var4);
            return new ResultVO(false, "删除时发生错误:" + var4.getMessage(), "500");
        }
    }

    /**
     * 根据参数分组名查询最大参数序号
     *
     * @param params groupname 参数分组名
     * @return
     * @auther zhangw
     * @date 2018/10/22 17:16
     */
    @ResponseBody
    @RequestMapping(value = {"/get/maxparamsn"}, method = {RequestMethod.POST})
    public ResultVO getVersion(@RequestBody Map<String, String> params) {
        ResultVO resultVO = new ResultVO();
        try{
            String groupname = params.get("groupname");
            String paramSn = baseTypeParamService.selectMaxParamSn(groupname);
            if(Tools.StringIsNullOrSpace(paramSn)){
                resultVO.setData("1");
            }else {
                resultVO.setData(Long.valueOf(paramSn) + 1);
            }
        }catch (Exception e){
            log.error("生成参数版本时出错：", e);
            resultVO.setSuccess(false);
            resultVO.setMessage("生成参数版本时出错：" + e.getMessage());
        }
        return resultVO;
    }

}
